<html>
 <head>
  <meta charset="utf-8"/>
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport"/>
  <title>
   用Apache Spark进行大数据处理：Spark SQL  | 数螺 | NAUT IDEA
  </title>
  <link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" rel="stylesheet"/>
  <link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
  <style type="text/css">
   #xmain img {
                  max-width: 100%;
                  display: block;
                  margin-top: 10px;
                  margin-bottom: 10px;
                }

                #xmain p {
                    line-height:150%;
                    font-size: 16px;
                    margin-top: 20px;
                }

                #xmain h2 {
                    font-size: 24px;
                }

                #xmain h3 {
                    font-size: 20px;
                }

                #xmain h4 {
                    font-size: 18px;
                }


                .header {
	           background-color: #0099ff;
	           color: #ffffff;
	           margin-bottom: 20px;
	        }

	        .header p {
                  margin: 0px;
                  padding: 10px 0;
                  display: inline-block;  
                  vertical-align: middle;
                  font-size: 16px;
               }

               .header a {
                 color: white;
               }

              .header img {
                 height: 25px;
              }
  </style>
  <script src="http://cdn.bootcss.com/jquery/3.0.0/jquery.min.js">
  </script>
  <script src="http://nautstatic-10007657.file.myqcloud.com/static/css/readability.min.js" type="text/javascript">
  </script>
  <script type="text/javascript">
   $(document).ready(function() {
                 var loc = document.location;
                 var uri = {
                  spec: "http://dataunion.org/19559.html",
                  host: "http://dataunion.org",
                  prePath: "http://dataunion.org",
                  scheme: "http",
                  pathBase: "http://dataunion.org/"
                 };
    
                 var documentClone = document.cloneNode(true);
                 var article = new Readability(uri, documentClone).parse();
     
                 document.getElementById("xmain").innerHTML = article.content;
                });
  </script>
  <!-- 1466459520: Accept with keywords: (title(0.5):数据处理,Spark,数盟,社区, topn(0.4):社区,数据类型,数盟,行业资讯,数据挖掘,数据分析,示例,数据源,数据库,模式,文章,数据处理,Spark,团队,数据,文本文件,对象,编程,客户,字符串,spark,语句,结构化,顺序,转化成,框架,方法,代码,结果,版本).-->
 </head>
 <body onload="">
  <div class="header">
   <div class="container">
    <div class="row">
     <div class="col-xs-6 col-sm-6 text-left">
      <a href="/databee">
       <img src="http://nautidea-10007657.cos.myqcloud.com/logo_white.png"/>
      </a>
      <a href="/databee">
       <p>
        数螺
       </p>
      </a>
     </div>
     <div class="hidden-xs col-sm-6 text-right">
      <p>
       致力于数据科学的推广和知识传播
      </p>
     </div>
    </div>
   </div>
  </div>
  <div class="container text-center">
   <h1>
    用Apache Spark进行大数据处理：Spark SQL
   </h1>
  </div>
  <div class="container" id="xmain">
   ﻿﻿
   <title>
    用Apache Spark进行大数据处理：Spark SQL | 数盟社区
   </title>
   <!-- All in One SEO Pack 2.2.7.6.2 by Michael Torbert of Semper Fi Web Design[32,77] -->
   <!-- /all in one seo pack -->
   <!--
<div align="center">
<a href="http://strata.oreilly.com.cn/hadoop-big-data-cn?cmp=mp-data-confreg-home-stcn16_dataunion_pc" target="_blank"><img src="http://dataunion.org/wp-content/uploads/2016/05/stratabj.jpg"/ ></a>
</div>
-->
   <header id="header-web">
    <div class="header-main">
     <hgroup class="logo">
      <h1>
       <a href="http://dataunion.org/" rel="home" title="数盟社区">
        <img src="http://dataunion.org/wp-content/themes/yzipi/images/logo.png"/>
       </a>
      </h1>
     </hgroup>
     <!--logo-->
     <nav class="header-nav">
      <ul class="menu" id="menu-%e4%b8%bb%e8%8f%9c%e5%8d%95">
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-71" id="menu-item-71">
        <a href="http://dataunion.org/category/events" title="events">
         活动
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-22457" id="menu-item-22457">
          <a href="http://dataunion.org/2016timeline">
           2016档期
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-22459" id="menu-item-22459">
          <a href="http://dataunion.org/category/parterc">
           合作会议
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category current-post-ancestor current-menu-parent current-post-parent menu-item-has-children menu-item-20869" id="menu-item-20869">
        <a href="http://dataunion.org/category/tech" title="articles">
         文章
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20867" id="menu-item-20867">
          <a href="http://dataunion.org/category/tech/base" title="base">
           基础架构
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3302" id="menu-item-3302">
          <a href="http://dataunion.org/category/tech/ai" title="ai">
           人工智能
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3303" id="menu-item-3303">
          <a href="http://dataunion.org/category/tech/analysis" title="analysis">
           数据分析
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21920" id="menu-item-21920">
          <a href="http://dataunion.org/category/tech/dm">
           数据挖掘
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3314" id="menu-item-3314">
          <a href="http://dataunion.org/category/tech/viz" title="viz">
           可视化
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3305" id="menu-item-3305">
          <a href="http://dataunion.org/category/tech/devl" title="devl">
           编程语言
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-20876" id="menu-item-20876">
        <a href="http://dataunion.org/category/industry">
         行业
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-16328" id="menu-item-16328">
          <a href="http://dataunion.org/category/industry/case" title="case">
           行业应用
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-2112" id="menu-item-2112">
          <a href="http://dataunion.org/category/industry/demo" title="demo">
           Demo展示
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21562" id="menu-item-21562">
          <a href="http://dataunion.org/category/industry/news">
           行业资讯
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-311" id="menu-item-311">
        <a href="http://dataunion.org/category/sources" title="sources">
         资源
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20870" id="menu-item-20870">
        <a href="http://dataunion.org/category/books" title="book">
         图书
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21363" id="menu-item-21363">
        <a href="http://dataunion.org/category/training">
         课程
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-21853" id="menu-item-21853">
        <a href="http://dataunion.org/category/jobs">
         职位
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-22050" id="menu-item-22050">
          <a href="http://dataunion.org/category/career">
           职业规划
          </a>
         </li>
        </ul>
       </li>
      </ul>
     </nav>
     <!--header-nav-->
    </div>
   </header>
   <!--header-web-->
   <div id="main">
    <div id="soutab">
     <form action="http://dataunion.org/" class="search" method="get">
     </form>
    </div>
    <div id="container">
     <nav id="mbx">
      当前位置：
      <a href="http://dataunion.org">
       首页
      </a>
      &gt;
      <a href="http://dataunion.org/category/tech">
       文章
      </a>
      &gt;  正文
     </nav>
     <!--mbx-->
     <article class="content">
      <header align="centre" class="contenttitle">
       <div class="mscc">
        <h1 class="mscctitle">
         <a href="http://dataunion.org/19559.html">
          用Apache Spark进行大数据处理：Spark SQL
         </a>
        </h1>
        <address class="msccaddress ">
         <em>
          1,215 次阅读 -
         </em>
         <a href="http://dataunion.org/category/tech" rel="category tag">
          文章
         </a>
        </address>
       </div>
      </header>
      <div class="content-text">
       <p>
        作者
        <a class="editorlink f_taxonomyEditor" href="http://www.infoq.com/cn/author/Srini-Penchikala">
         Srini Penchikala
        </a>
        ，译者
        <strong>
         <a class="editorlink f_taxonomyTranslator" href="http://www.infoq.com/cn/author/%E4%B8%9B%E4%B8%80">
          丛一
         </a>
        </strong>
        出处：
        <a href="http://www.infoq.com/cn/articles/apache-spark-sql">
         infoq
        </a>
       </p>
       <p>
        在Apache Spark文章系列的
        <a href="http://www.infoq.com/articles/apache-spark-introduction">
         前一篇文章
        </a>
        中，我们学习了什么是Apache Spark框架，以及如何用该框架帮助组织处理大数据处理分析的需求。
       </p>
       <p>
        Spark SQL，作为Apache Spark大数据框架的一部分，主要用于结构化数据处理和对Spark数据执行类SQL的查询。通过Spark SQL，可以针对不同格式的数据执行ETL操作（如JSON，Parquet，数据库）然后完成特定的查询操作。
       </p>
       <p>
        在这一文章系列的第二篇中，我们将讨论Spark SQL库，如何使用Spark SQL库对存储在批处理文件、JSON数据集或Hive表中的数据执行SQL查询。
       </p>
       <div class="clear">
       </div>
       <div class="related_sponsors visible stacked">
       </div>
       <p>
        Spark大数据处理框架目前最新的版本是上个月发布的Spark 1.3。这一版本之前，Spark SQL模块一直处于“Alpha”状态，现在该团队已经从Spark SQL库上将这一标签移除。这一版本中包含了许多新的功能特性，其中一部分如下：
       </p>
       <ul>
        <li>
         <b>
          数据框架（DataFrame
         </b>
         <b>
          ）：
         </b>
         Spark新版本中提供了可以作为分布式SQL查询引擎的程序化抽象DataFrame。
        </li>
        <li>
         <b>
          数据源（Data Sources
         </b>
         <b>
          ）：
         </b>
         随着数据源API的增加，Spark SQL可以便捷地处理以多种不同格式存储的结构化数据，如Parquet，JSON以及Apache Avro库。
        </li>
        <li>
         <b>
          JDBC
         </b>
         <b>
          服务器（JDBC Server
         </b>
         <b>
          ）：
         </b>
         内置的JDBC服务器可以便捷地连接到存储在关系型数据库表中的结构化数据并利用传统的商业智能（BI）工具进行大数据分析。
        </li>
       </ul>
       <h2>
        Spark SQL组件
       </h2>
       <p>
        使用Spark SQL时，最主要的两个组件就是DataFrame和SQLContext。
       </p>
       <p>
        首先，我们来了解一下DataFrame。
       </p>
       <div id="lowerFullwidthVCR">
       </div>
       <h3>
        DataFrame
       </h3>
       <p>
        <a href="https://spark.apache.org/docs/1.3.0/api/scala/index.html#org.apache.spark.sql.DataFrame">
         DataFrame
        </a>
        是一个分布式的，按照命名列的形式组织的数据集合。DataFrame基于R语言中的data frame概念，与关系型数据库中的数据库表类似。
       </p>
       <p>
        之前版本的Spark SQL API中的SchemaRDD已经更名为DataFrame。
       </p>
       <p>
        通过调用将DataFrame的内容作为行RDD（RDD of Rows）返回的
        <a href="https://spark.apache.org/docs/1.3.0/api/scala/index.html#org.apache.spark.sql.DataFrame">
         rdd方法
        </a>
        ，可以将DataFrame转换成RDD。
       </p>
       <p>
        可以通过如下数据源创建DataFrame：
       </p>
       <ul>
        <li>
         已有的RDD
        </li>
        <li>
         结构化数据文件
        </li>
        <li>
         JSON数据集
        </li>
        <li>
         Hive表
        </li>
        <li>
         外部数据库
        </li>
       </ul>
       <p>
        Spark SQL和DataFrame API已经在下述几种程序设计语言中实现：
       </p>
       <ul>
        <li>
         Scala（https://spark.apache.org/docs/1.3.0/api/scala/index.html#org.apache.spark.sql.package）
        </li>
        <li>
         Java（https://spark.apache.org/docs/1.3.0/api/java/index.html?org/apache/spark/sql/api/java/package-summary.html）
        </li>
        <li>
         Python（https://spark.apache.org/docs/1.3.0/api/python/pyspark.sql.html）
        </li>
       </ul>
       <p>
        本文中所涉及的Spark SQL代码示例均使用Spark Scala Shell程序。
       </p>
       <h3>
        SQLContext
       </h3>
       <p>
        Spark SQL提供
        <a href="http://spark.apache.org/docs/latest/api/scala/index.html#org.apache.spark.sql.SQLContext">
         SQLContext
        </a>
        封装Spark中的所有关系型功能。可以用之前的示例中的现有SparkContext创建SQLContext。下述代码片段展示了如何创建一个SQLContext对象。
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768657eaaa77452578123" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          val sqlContext = new org.apache.spark.sql.SQLContext(sc)
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-5768657eaaa77452578123-1">
               1
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-5768657eaaa77452578123-1">
               <span class="crayon-e">
                val
               </span>
               <span class="crayon-v">
                sqlContext
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-r">
                new
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                org
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                apache
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                spark
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                sql
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                SQLContext
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                sc
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0118 seconds] -->
       <p>
        此外，Spark SQL中的
        <a href="https://spark.apache.org/docs/1.3.0/api/scala/index.html#org.apache.spark.sql.hive.HiveContext">
         HiveContext
        </a>
        可以提供SQLContext所提供功能的超集。可以在用HiveQL解析器编写查询语句以及从Hive表中读取数据时使用。
       </p>
       <p>
        在Spark程序中使用HiveContext无需既有的Hive环境。
       </p>
       <h3>
        JDBC数据源
       </h3>
       <p>
        Spark SQL库的其他功能还包括数据源，如JDBC数据源。
       </p>
       <p>
        JDBC数据源可用于通过JDBC API读取关系型数据库中的数据。相比于使用
        <a href="https://spark.apache.org/docs/1.3.0/api/scala/index.html#org.apache.spark.rdd.JdbcRDD">
         JdbcRDD
        </a>
        ，应该将JDBC数据源的方式作为首选，因为JDBC数据源能够将结果作为DataFrame对象返回，直接用Spark SQL处理或与其他数据源连接。
       </p>
       <h3>
        Spark SQL示例应用
       </h3>
       <p>
        在上一篇文章中，我们学习了如何在本地环境中安装Spark框架，如何启动Spark框架并用Spark Scala Shell与其交互。如需安装最新版本的Spark，可以从Spark
        <a href="http://spark.apache.org/downloads.html">
         网站
        </a>
        下载该软件。
       </p>
       <p>
        对于本文中的代码示例，我们将使用相同的Spark Shell执行Spark SQL程序。这些代码示例适用于Windows环境。
       </p>
       <p>
        为了确保Spark Shell程序有足够的内存，可以在运行spark-shell命令时，加入driver-memory命令行参数，如下所示：
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768657eaaa8b153813638" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          spark-shell.cmd --driver-memory 1G
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-5768657eaaa8b153813638-1">
               1
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-5768657eaaa8b153813638-1">
               <span class="crayon-v">
                spark
               </span>
               <span class="crayon-o">
                -
               </span>
               <span class="crayon-v">
                shell
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                cmd
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                --
               </span>
               <span class="crayon-v">
                driver
               </span>
               <span class="crayon-o">
                -
               </span>
               <span class="crayon-i">
                memory
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                1G
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0010 seconds] -->
       <p>
       </p>
       <h3>
        Spark SQL应用
       </h3>
       <p>
        Spark Shell启动后，就可以用Spark SQL API执行数据分析查询。
       </p>
       <p>
        在第一个示例中，我们将从文本文件中加载用户数据并从数据集中创建一个DataFrame对象。然后运行DataFrame函数，执行特定的数据选择查询。
       </p>
       <p>
        文本文件customers.txt中的内容如下：
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768657eaaa92852113672" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          100, John Smith, Austin, TX, 78727
200, Joe Johnson, Dallas, TX, 75201
300, Bob Jones, Houston, TX, 77028
400, Andy Davis, San Antonio, TX, 78227
500, James Williams, Austin, TX, 78727
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-5768657eaaa92852113672-1">
               1
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaa92852113672-2">
               2
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaa92852113672-3">
               3
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaa92852113672-4">
               4
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaa92852113672-5">
               5
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-5768657eaaa92852113672-1">
               <span class="crayon-cn">
                100
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                John
               </span>
               <span class="crayon-v">
                Smith
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                Austin
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                TX
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                78727
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaa92852113672-2">
               <span class="crayon-cn">
                200
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                Joe
               </span>
               <span class="crayon-v">
                Johnson
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                Dallas
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                TX
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                75201
               </span>
              </div>
              <div class="crayon-line" id="crayon-5768657eaaa92852113672-3">
               <span class="crayon-cn">
                300
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                Bob
               </span>
               <span class="crayon-v">
                Jones
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                Houston
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                TX
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                77028
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaa92852113672-4">
               <span class="crayon-cn">
                400
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                Andy
               </span>
               <span class="crayon-v">
                Davis
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                San
               </span>
               <span class="crayon-v">
                Antonio
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                TX
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                78227
               </span>
              </div>
              <div class="crayon-line" id="crayon-5768657eaaa92852113672-5">
               <span class="crayon-cn">
                500
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                James
               </span>
               <span class="crayon-v">
                Williams
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                Austin
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                TX
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                78727
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0221 seconds] -->
       <p>
        下述代码片段展示了可以在Spark Shell终端执行的Spark SQL命令。
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768657eaaa98330470517" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          // 首先用已有的Spark Context对象创建SQLContext对象
val sqlContext = new org.apache.spark.sql.SQLContext(sc)

// 导入语句，可以隐式地将RDD转化成DataFrame
import sqlContext.implicits._

// 创建一个表示客户的自定义类
case class Customer(customer_id: Int, name: String, city: String, state: String, zip_code: String)

// 用数据集文本文件创建一个Customer对象的DataFrame
val dfCustomers = sc.textFile("data/customers.txt").map(_.split(",")).map(p =&amp;gt; Customer(p(0).trim.toInt, p(1), p(2), p(3), p(4))).toDF()

// 将DataFrame注册为一个表
dfCustomers.registerTempTable("customers")

// 显示DataFrame的内容
dfCustomers.show()

// 打印DF模式
dfCustomers.printSchema()

// 选择客户名称列
dfCustomers.select("name").show()

// 选择客户名称和城市列
dfCustomers.select("name", "city").show()

// 根据id选择客户
dfCustomers.filter(dfCustomers("customer_id").equalTo(500)).show()

// 根据邮政编码统计客户数量
dfCustomers.groupBy("zip_code").count().show()
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-5768657eaaa98330470517-1">
               1
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaa98330470517-2">
               2
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaa98330470517-3">
               3
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaa98330470517-4">
               4
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaa98330470517-5">
               5
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaa98330470517-6">
               6
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaa98330470517-7">
               7
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaa98330470517-8">
               8
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaa98330470517-9">
               9
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaa98330470517-10">
               10
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaa98330470517-11">
               11
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaa98330470517-12">
               12
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaa98330470517-13">
               13
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaa98330470517-14">
               14
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaa98330470517-15">
               15
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaa98330470517-16">
               16
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaa98330470517-17">
               17
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaa98330470517-18">
               18
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaa98330470517-19">
               19
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaa98330470517-20">
               20
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaa98330470517-21">
               21
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaa98330470517-22">
               22
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaa98330470517-23">
               23
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaa98330470517-24">
               24
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaa98330470517-25">
               25
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaa98330470517-26">
               26
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaa98330470517-27">
               27
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaa98330470517-28">
               28
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaa98330470517-29">
               29
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaa98330470517-30">
               30
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaa98330470517-31">
               31
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaa98330470517-32">
               32
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-5768657eaaa98330470517-1">
               <span class="crayon-c">
                // 首先用已有的Spark Context对象创建SQLContext对象
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaa98330470517-2">
               <span class="crayon-e">
                val
               </span>
               <span class="crayon-v">
                sqlContext
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-r">
                new
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                org
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                apache
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                spark
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                sql
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                SQLContext
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                sc
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-5768657eaaa98330470517-3">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaa98330470517-4">
               <span class="crayon-c">
                // 导入语句，可以隐式地将RDD转化成DataFrame
               </span>
              </div>
              <div class="crayon-line" id="crayon-5768657eaaa98330470517-5">
               <span class="crayon-e">
                import
               </span>
               <span class="crayon-v">
                sqlContext
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                implicits
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                _
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaa98330470517-6">
              </div>
              <div class="crayon-line" id="crayon-5768657eaaa98330470517-7">
               <span class="crayon-c">
                // 创建一个表示客户的自定义类
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaa98330470517-8">
               <span class="crayon-st">
                case
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                Customer
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                customer_id
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-t">
                Int
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                name
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-t">
                String
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                city
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-t">
                String
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                state
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-t">
                String
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                zip_code
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-t">
                String
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-5768657eaaa98330470517-9">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaa98330470517-10">
               <span class="crayon-c">
                // 用数据集文本文件创建一个Customer对象的DataFrame
               </span>
              </div>
              <div class="crayon-line" id="crayon-5768657eaaa98330470517-11">
               <span class="crayon-e">
                val
               </span>
               <span class="crayon-v">
                dfCustomers
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                sc
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                textFile
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-s">
                "data/customers.txt"
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                map
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                _
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                split
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-s">
                ","
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                map
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                p
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =&amp;
               </span>
               <span class="crayon-v">
                gt
               </span>
               <span class="crayon-sy">
                ;
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                Customer
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-e">
                p
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                trim
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                toInt
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                p
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                p
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                2
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                p
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                3
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                p
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                4
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                toDF
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaa98330470517-12">
              </div>
              <div class="crayon-line" id="crayon-5768657eaaa98330470517-13">
               <span class="crayon-c">
                // 将DataFrame注册为一个表
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaa98330470517-14">
               <span class="crayon-v">
                dfCustomers
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                registerTempTable
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-s">
                "customers"
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-5768657eaaa98330470517-15">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaa98330470517-16">
               <span class="crayon-c">
                // 显示DataFrame的内容
               </span>
              </div>
              <div class="crayon-line" id="crayon-5768657eaaa98330470517-17">
               <span class="crayon-v">
                dfCustomers
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                show
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaa98330470517-18">
              </div>
              <div class="crayon-line" id="crayon-5768657eaaa98330470517-19">
               <span class="crayon-c">
                // 打印DF模式
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaa98330470517-20">
               <span class="crayon-v">
                dfCustomers
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                printSchema
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-5768657eaaa98330470517-21">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaa98330470517-22">
               <span class="crayon-c">
                // 选择客户名称列
               </span>
              </div>
              <div class="crayon-line" id="crayon-5768657eaaa98330470517-23">
               <span class="crayon-v">
                dfCustomers
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                select
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-s">
                "name"
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                show
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaa98330470517-24">
              </div>
              <div class="crayon-line" id="crayon-5768657eaaa98330470517-25">
               <span class="crayon-c">
                // 选择客户名称和城市列
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaa98330470517-26">
               <span class="crayon-v">
                dfCustomers
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                select
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-s">
                "name"
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-s">
                "city"
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                show
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-5768657eaaa98330470517-27">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaa98330470517-28">
               <span class="crayon-c">
                // 根据id选择客户
               </span>
              </div>
              <div class="crayon-line" id="crayon-5768657eaaa98330470517-29">
               <span class="crayon-v">
                dfCustomers
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                filter
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-e">
                dfCustomers
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-s">
                "customer_id"
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                equalTo
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                500
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                show
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaa98330470517-30">
              </div>
              <div class="crayon-line" id="crayon-5768657eaaa98330470517-31">
               <span class="crayon-c">
                // 根据邮政编码统计客户数量
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaa98330470517-32">
               <span class="crayon-v">
                dfCustomers
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                groupBy
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-s">
                "zip_code"
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                count
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                show
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0476 seconds] -->
       <p>
        在上一示例中，模式是通过反射而得来的。我们也可以通过编程的方式指定数据集的模式。这种方法在由于数据的结构以字符串的形式编码而无法提前定义定制类的情况下非常实用。
       </p>
       <p>
        如下代码示例展示了如何使用新的数据类型类StructType，StringType和StructField指定模式。
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768657eaaaa0285936421" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          //
// 用编程的方式指定模式
//

// 用已有的Spark Context对象创建SQLContext对象
val sqlContext = new org.apache.spark.sql.SQLContext(sc)

// 创建RDD对象
val rddCustomers = sc.textFile("data/customers.txt")

// 用字符串编码模式
val schemaString = "customer_id name city state zip_code"

// 导入Spark SQL数据类型和Row
import org.apache.spark.sql._

import org.apache.spark.sql.types._;

// 用模式字符串生成模式对象
val schema = StructType(schemaString.split(" ").map(fieldName =&amp;gt; StructField(fieldName, StringType, true)))

// 将RDD（rddCustomers）记录转化成Row。
val rowRDD = rddCustomers.map(_.split(",")).map(p =&amp;gt; Row(p(0).trim,p(1),p(2),p(3),p(4)))

// 将模式应用于RDD对象。
val dfCustomers = sqlContext.createDataFrame(rowRDD, schema)

// 将DataFrame注册为表
dfCustomers.registerTempTable("customers")

// 用sqlContext对象提供的sql方法执行SQL语句。
val custNames = sqlContext.sql("SELECT name FROM customers")

// SQL查询的返回结果为DataFrame对象，支持所有通用的RDD操作。
// 可以按照顺序访问结果行的各个列。
custNames.map(t =&amp;gt; "Name: " + t(0)).collect().foreach(println)

// 用sqlContext对象提供的sql方法执行SQL语句。
val customersByCity = sqlContext.sql("SELECT name,zip_code FROM customers ORDER BY zip_code")

// SQL查询的返回结果为DataFrame对象，支持所有通用的RDD操作。
// 可以按照顺序访问结果行的各个列。
customersByCity.map(t =&amp;gt; t(0) + "," + t(1)).collect().foreach(println)
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-5768657eaaaa0285936421-1">
               1
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaaa0285936421-2">
               2
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaaa0285936421-3">
               3
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaaa0285936421-4">
               4
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaaa0285936421-5">
               5
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaaa0285936421-6">
               6
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaaa0285936421-7">
               7
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaaa0285936421-8">
               8
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaaa0285936421-9">
               9
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaaa0285936421-10">
               10
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaaa0285936421-11">
               11
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaaa0285936421-12">
               12
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaaa0285936421-13">
               13
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaaa0285936421-14">
               14
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaaa0285936421-15">
               15
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaaa0285936421-16">
               16
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaaa0285936421-17">
               17
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaaa0285936421-18">
               18
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaaa0285936421-19">
               19
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaaa0285936421-20">
               20
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaaa0285936421-21">
               21
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaaa0285936421-22">
               22
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaaa0285936421-23">
               23
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaaa0285936421-24">
               24
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaaa0285936421-25">
               25
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaaa0285936421-26">
               26
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaaa0285936421-27">
               27
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaaa0285936421-28">
               28
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaaa0285936421-29">
               29
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaaa0285936421-30">
               30
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaaa0285936421-31">
               31
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaaa0285936421-32">
               32
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaaa0285936421-33">
               33
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaaa0285936421-34">
               34
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaaa0285936421-35">
               35
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaaa0285936421-36">
               36
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaaa0285936421-37">
               37
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaaa0285936421-38">
               38
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaaa0285936421-39">
               39
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaaa0285936421-40">
               40
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaaa0285936421-41">
               41
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-5768657eaaaa0285936421-42">
               42
              </div>
              <div class="crayon-num" data-line="crayon-5768657eaaaa0285936421-43">
               43
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-5768657eaaaa0285936421-1">
               <span class="crayon-c">
                //
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaaa0285936421-2">
               <span class="crayon-c">
                // 用编程的方式指定模式
               </span>
              </div>
              <div class="crayon-line" id="crayon-5768657eaaaa0285936421-3">
               <span class="crayon-c">
                //
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaaa0285936421-4">
              </div>
              <div class="crayon-line" id="crayon-5768657eaaaa0285936421-5">
               <span class="crayon-c">
                // 用已有的Spark Context对象创建SQLContext对象
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaaa0285936421-6">
               <span class="crayon-e">
                val
               </span>
               <span class="crayon-v">
                sqlContext
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-r">
                new
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                org
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                apache
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                spark
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                sql
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                SQLContext
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                sc
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-5768657eaaaa0285936421-7">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaaa0285936421-8">
               <span class="crayon-c">
                // 创建RDD对象
               </span>
              </div>
              <div class="crayon-line" id="crayon-5768657eaaaa0285936421-9">
               <span class="crayon-e">
                val
               </span>
               <span class="crayon-v">
                rddCustomers
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                sc
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                textFile
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-s">
                "data/customers.txt"
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaaa0285936421-10">
              </div>
              <div class="crayon-line" id="crayon-5768657eaaaa0285936421-11">
               <span class="crayon-c">
                // 用字符串编码模式
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaaa0285936421-12">
               <span class="crayon-e">
                val
               </span>
               <span class="crayon-v">
                schemaString
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-s">
                "customer_id name city state zip_code"
               </span>
              </div>
              <div class="crayon-line" id="crayon-5768657eaaaa0285936421-13">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaaa0285936421-14">
               <span class="crayon-c">
                // 导入Spark SQL数据类型和Row
               </span>
              </div>
              <div class="crayon-line" id="crayon-5768657eaaaa0285936421-15">
               <span class="crayon-e">
                import
               </span>
               <span class="crayon-v">
                org
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                apache
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                spark
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                sql
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-i">
                _
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaaa0285936421-16">
              </div>
              <div class="crayon-line" id="crayon-5768657eaaaa0285936421-17">
               <span class="crayon-e">
                import
               </span>
               <span class="crayon-v">
                org
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                apache
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                spark
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                sql
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                types
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                _
               </span>
               <span class="crayon-sy">
                ;
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaaa0285936421-18">
              </div>
              <div class="crayon-line" id="crayon-5768657eaaaa0285936421-19">
               <span class="crayon-c">
                // 用模式字符串生成模式对象
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaaa0285936421-20">
               <span class="crayon-e">
                val
               </span>
               <span class="crayon-v">
                schema
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                StructType
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                schemaString
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                split
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-s">
                " "
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                map
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                fieldName
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =&amp;
               </span>
               <span class="crayon-v">
                gt
               </span>
               <span class="crayon-sy">
                ;
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                StructField
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                fieldName
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                StringType
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-t">
                true
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-5768657eaaaa0285936421-21">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaaa0285936421-22">
               <span class="crayon-c">
                // 将RDD（rddCustomers）记录转化成Row。
               </span>
              </div>
              <div class="crayon-line" id="crayon-5768657eaaaa0285936421-23">
               <span class="crayon-e">
                val
               </span>
               <span class="crayon-v">
                rowRDD
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                rddCustomers
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                map
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                _
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                split
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-s">
                ","
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                map
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                p
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =&amp;
               </span>
               <span class="crayon-v">
                gt
               </span>
               <span class="crayon-sy">
                ;
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                Row
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-e">
                p
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                trim
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-e">
                p
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-e">
                p
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                2
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-e">
                p
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                3
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-e">
                p
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                4
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaaa0285936421-24">
              </div>
              <div class="crayon-line" id="crayon-5768657eaaaa0285936421-25">
               <span class="crayon-c">
                // 将模式应用于RDD对象。
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaaa0285936421-26">
               <span class="crayon-e">
                val
               </span>
               <span class="crayon-v">
                dfCustomers
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                sqlContext
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                createDataFrame
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                rowRDD
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                schema
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-5768657eaaaa0285936421-27">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaaa0285936421-28">
               <span class="crayon-c">
                // 将DataFrame注册为表
               </span>
              </div>
              <div class="crayon-line" id="crayon-5768657eaaaa0285936421-29">
               <span class="crayon-v">
                dfCustomers
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                registerTempTable
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-s">
                "customers"
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaaa0285936421-30">
              </div>
              <div class="crayon-line" id="crayon-5768657eaaaa0285936421-31">
               <span class="crayon-c">
                // 用sqlContext对象提供的sql方法执行SQL语句。
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaaa0285936421-32">
               <span class="crayon-e">
                val
               </span>
               <span class="crayon-v">
                custNames
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                sqlContext
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                sql
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-s">
                "SELECT name FROM customers"
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-5768657eaaaa0285936421-33">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaaa0285936421-34">
               <span class="crayon-c">
                // SQL查询的返回结果为DataFrame对象，支持所有通用的RDD操作。
               </span>
              </div>
              <div class="crayon-line" id="crayon-5768657eaaaa0285936421-35">
               <span class="crayon-c">
                // 可以按照顺序访问结果行的各个列。
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaaa0285936421-36">
               <span class="crayon-v">
                custNames
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                map
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                t
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =&amp;
               </span>
               <span class="crayon-v">
                gt
               </span>
               <span class="crayon-sy">
                ;
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-s">
                "Name: "
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                +
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                t
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                collect
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-st">
                foreach
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                println
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-5768657eaaaa0285936421-37">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaaa0285936421-38">
               <span class="crayon-c">
                // 用sqlContext对象提供的sql方法执行SQL语句。
               </span>
              </div>
              <div class="crayon-line" id="crayon-5768657eaaaa0285936421-39">
               <span class="crayon-e">
                val
               </span>
               <span class="crayon-v">
                customersByCity
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                sqlContext
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                sql
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-s">
                "SELECT name,zip_code FROM customers ORDER BY zip_code"
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaaa0285936421-40">
              </div>
              <div class="crayon-line" id="crayon-5768657eaaaa0285936421-41">
               <span class="crayon-c">
                // SQL查询的返回结果为DataFrame对象，支持所有通用的RDD操作。
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-5768657eaaaa0285936421-42">
               <span class="crayon-c">
                // 可以按照顺序访问结果行的各个列。
               </span>
              </div>
              <div class="crayon-line" id="crayon-5768657eaaaa0285936421-43">
               <span class="crayon-v">
                customersByCity
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                map
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                t
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =&amp;
               </span>
               <span class="crayon-v">
                gt
               </span>
               <span class="crayon-sy">
                ;
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                t
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                +
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-s">
                ","
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                +
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                t
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                collect
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-st">
                foreach
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                println
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0424 seconds] -->
       <p>
        除了文本文件之外，也可以从其他数据源中加载数据，如JSON数据文件，Hive表，甚至可以通过JDBC数据源加载关系型数据库表中的数据。
       </p>
       <p>
        如上所示，Spark SQL提供了十分友好的SQL接口，可以与来自多种不同数据源的数据进行交互，而且所采用的语法也是团队熟知的SQL查询语法。这对于非技术类的项目成员，如数据分析师以及数据库管理员来说，非常实用。
       </p>
       <h2>
        总结
       </h2>
       <p>
        本文中，我们了解到Apache Spark SQL如何用熟知的SQL查询语法提供与Spark数据交互的SQL接口。Spark SQL是一个功能强大的库，组织中的非技术团队成员，如业务分析师和数据分析师，都可以用Spark SQL执行数据分析。
       </p>
       <p>
        下一篇文章中，我们将讨论可用于处理实时数据或流数据的
        <a href="http://spark.apache.org/streaming/">
         Spark Streaming库
        </a>
        。Spark Streaming库是任何一个组织的整体数据处理和管理生命周期中另外一个重要的组成部分，因为流数据处理可为我们提供对系统的实时观察。这对于欺诈检测、在线交易系统、事件处理解决方案等用例来说至关重要。
       </p>
       <h2>
        参考文献
       </h2>
       <ul>
        <li>
         <a href="http://spark.apache.org/">
          Spark主站
         </a>
        </li>
        <li>
         <a href="https://spark.apache.org/sql/">
          Spark SQL网站
         </a>
        </li>
        <li>
         <a href="https://spark.apache.org/docs/latest/sql-programming-guide.html">
          Spark SQL程序设计指南
         </a>
        </li>
        <li>
         <a href="http://www.infoq.com/articles/apache-spark-introduction">
          用Apache Spark进行大数据处理——第一部分：入门介绍
         </a>
        </li>
       </ul>
       <h2>
        关于作者
       </h2>
       <p>
        <b>
         Srini Penchikala
        </b>
        目前是一家金融服务机构的软件架构师，这个机构位于德克萨斯州的奥斯汀。他在软件系统架构、设计和开发方面有超过20年的经验。Srini目前正在撰写一本关于NoSQL数据库模式的书。他还是曼宁出版社出版的《Spring Roo in Action》一书的合著者（http://www.manning.com/SpringRooinAction）。他还曾经出席各种会议，如JavaOne，SEI Architecture Technology Conference（SATURN），IT Architect Conference（ITARC），No Fluff Just Stuff，NoSQL Now和Project World Conference等。Srini还在InfoQ，The ServerSide，OReilly Network（ONJava），DevX Java，java.net以及JavaWorld等网站上发表过很多关于软件系统架构、安全和风险管理以及NoSQL数据库等方面的文章。他还是InfoQ NoSQL数据库社区的责任编辑（http://www.infoq.com/author/Srini-Penchikala）。
       </p>
       <div>
       </div>
       <p>
        <strong>
         查看英文原文：
        </strong>
        <a href="http://www.infoq.com/articles/apache-spark-sql">
         Big Data Processing with Apache Spark – Part 2: Spark SQL
        </a>
       </p>
      </div>
      <div>
       <strong>
        注：转载文章均来自于公开网络，仅供学习使用，不会用于任何商业用途，如果侵犯到原作者的权益，请您与我们联系删除或者授权事宜，联系邮箱：contact@dataunion.org。转载数盟网站文章请注明原文章作者，否则产生的任何版权纠纷与数盟无关。
       </strong>
      </div>
      <!--content_text-->
      <div class="fenxian">
       <!-- JiaThis Button BEGIN -->
       <div class="jiathis_style_32x32">
        <p class="jiathis_button_weixin">
        </p>
        <p class="jiathis_button_tsina">
        </p>
        <p class="jiathis_button_qzone">
        </p>
        <p class="jiathis_button_cqq">
        </p>
        <p class="jiathis_button_tumblr">
        </p>
        <a class="jiathis jiathis_txt jtico jtico_jiathis" href="http://www.jiathis.com/share" target="_blank">
        </a>
        <p class="jiathis_counter_style">
        </p>
       </div>
       <!-- JiaThis Button END -->
      </div>
     </article>
     <!--content-->
     <!--相关文章-->
     <div class="xianguan">
      <div class="xianguantitle">
       相关文章！
      </div>
      <ul class="pic">
       <li>
        <a href="http://dataunion.org/20824.html">
         <img src="http://dataunion.org/wp-content/uploads/2015/09/t018630756a7e263b33-300x165.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/20824.html" rel="bookmark" title="如何判断一笔交易是否属于欺诈？你只是需要一点数据挖掘">
         如何判断一笔交易是否属于欺诈？你只是需要一点数据挖掘
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/20820.html">
         <img src="http://dataunion.org/wp-content/uploads/2015/09/1-300x200.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/20820.html" rel="bookmark" title="人们对Python在企业级开发中的10大误解">
         人们对Python在企业级开发中的10大误解
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/20811.html">
         <img src="http://dataunion.org/wp-content/uploads/2015/09/t0133fcacae8523307b_副本-300x200.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/20811.html" rel="bookmark" title="大神亲传：26条深度学习的金科玉律！">
         大神亲传：26条深度学习的金科玉律！
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/20808.html">
         <img src="http://dataunion.org/wp-content/uploads/2015/09/640.webp-11-300x137.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/20808.html" rel="bookmark" title="我们是如何在一张地图上表现86万个数据的">
         我们是如何在一张地图上表现86万个数据的
        </a>
       </li>
      </ul>
     </div>
     <!--相关文章-->
     <div class="comment" id="comments">
      <!-- You can start editing here. -->
      <!-- If comments are open, but there are no comments. -->
      <div class="title">
       期待你一针见血的评论，Come on！
      </div>
      <div id="respond">
       <p>
        不用想啦，马上
        <a href="http://dataunion.org/wp-login.php?redirect_to=http%3A%2F%2Fdataunion.org%2F19559.html">
         "登录"
        </a>
        发表自已的想法.
       </p>
      </div>
     </div>
     <!-- .nav-single -->
    </div>
    <!--Container End-->
    <aside id="sitebar">
     <div class="sitebar_list2">
      <div class="wptag">
       <span class="tagtitle">
        热门标签+
       </span>
       <div class="tagg">
        <ul class="menu" id="menu-%e5%8f%8b%e6%83%85%e9%93%be%e6%8e%a5">
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1605" id="menu-item-1605">
          <a href="http://taidizh.com/">
           泰迪智慧
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-20884" id="menu-item-20884">
          <a href="http://www.transwarp.cn/">
           星环科技
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-3538" id="menu-item-3538">
          <a href="http://datall.org/">
           珈和遥感
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-20888" id="menu-item-20888">
          <a href="http://www.chinahadoop.cn/">
           小象学院
          </a>
         </li>
        </ul>
       </div>
      </div>
     </div>
     <div class="sitebar_list">
      <div class="textwidget">
       <div align="center">
        <a href="http://study.163.com/course/courseMain.htm?courseId=991022" target="_blank">
         <img src="http://dataunion.org/wp-content/uploads/2016/03/dv.jpg"/>
        </a>
       </div>
      </div>
     </div>
     <div class="sitebar_list">
      <h4 class="sitebar_title">
       文章分类
      </h4>
      <div class="tagcloud">
       <a class="tag-link-44" href="http://dataunion.org/category/industry/demo" style="font-size: 10.204724409449pt;" title="4个话题">
        Demo展示
       </a>
       <a class="tag-link-31" href="http://dataunion.org/category/experts" style="font-size: 15.826771653543pt;" title="52个话题">
        专家团队
       </a>
       <a class="tag-link-870" href="http://dataunion.org/category/tech/ai" style="font-size: 19.795275590551pt;" title="273个话题">
        人工智能
       </a>
       <a class="tag-link-488" href="http://dataunion.org/category/%e5%8a%a0%e5%85%a5%e6%95%b0%e7%9b%9f" style="font-size: 8pt;" title="1个话题">
        加入数盟
       </a>
       <a class="tag-link-869" href="http://dataunion.org/category/tech/viz" style="font-size: 17.204724409449pt;" title="93个话题">
        可视化
       </a>
       <a class="tag-link-30" href="http://dataunion.org/category/partners" style="font-size: 10.645669291339pt;" title="5个话题">
        合作伙伴
       </a>
       <a class="tag-link-889" href="http://dataunion.org/category/parterc" style="font-size: 11.582677165354pt;" title="8个话题">
        合作会议
       </a>
       <a class="tag-link-104" href="http://dataunion.org/category/books" style="font-size: 12.96062992126pt;" title="15个话题">
        图书
       </a>
       <a class="tag-link-220" href="http://dataunion.org/category/tech/base" style="font-size: 19.850393700787pt;" title="281个话题">
        基础架构
       </a>
       <a class="tag-link-219" href="http://dataunion.org/category/tech/analysis" style="font-size: 19.409448818898pt;" title="232个话题">
        数据分析
       </a>
       <a class="tag-link-887" href="http://dataunion.org/category/tech/dm" style="font-size: 13.291338582677pt;" title="17个话题">
        数据挖掘
       </a>
       <a class="tag-link-34" href="http://dataunion.org/category/tech" style="font-size: 20.732283464567pt;" title="404个话题">
        文章
       </a>
       <a class="tag-link-1" href="http://dataunion.org/category/uncategorized" style="font-size: 22pt;" title="693个话题">
        未分类
       </a>
       <a class="tag-link-4" href="http://dataunion.org/category/events" style="font-size: 14.503937007874pt;" title="29个话题">
        活动
       </a>
       <a class="tag-link-890" href="http://dataunion.org/category/tech/%e6%b7%b1%e5%ba%a6%e5%ad%a6%e4%b9%a0" style="font-size: 10.204724409449pt;" title="4个话题">
        深度学习
       </a>
       <a class="tag-link-221" href="http://dataunion.org/category/tech/devl" style="font-size: 18.968503937008pt;" title="193个话题">
        编程语言
       </a>
       <a class="tag-link-888" href="http://dataunion.org/category/career" style="font-size: 15.661417322835pt;" title="48个话题">
        职业规划
       </a>
       <a class="tag-link-5" href="http://dataunion.org/category/jobs" style="font-size: 14.11811023622pt;" title="25个话题">
        职位
       </a>
       <a class="tag-link-871" href="http://dataunion.org/category/industry" style="font-size: 15.716535433071pt;" title="49个话题">
        行业
       </a>
       <a class="tag-link-613" href="http://dataunion.org/category/industry/case" style="font-size: 16.984251968504pt;" title="84个话题">
        行业应用
       </a>
       <a class="tag-link-885" href="http://dataunion.org/category/industry/news" style="font-size: 17.425196850394pt;" title="102个话题">
        行业资讯
       </a>
       <a class="tag-link-10" href="http://dataunion.org/category/training" style="font-size: 14.228346456693pt;" title="26个话题">
        课程
       </a>
       <a class="tag-link-16" href="http://dataunion.org/category/sources" style="font-size: 15.661417322835pt;" title="48个话题">
        资源
       </a>
      </div>
     </div>
     <div class="sitebar_list">
      <h4 class="sitebar_title">
       功能
      </h4>
      <ul>
       <li>
        <a href="http://dataunion.org/wp-login.php?action=register">
         注册
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/wp-login.php">
         登录
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/feed">
         文章
         <abbr title="Really Simple Syndication">
          RSS
         </abbr>
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/comments/feed">
         评论
         <abbr title="Really Simple Syndication">
          RSS
         </abbr>
        </a>
       </li>
       <li>
        <a href="https://cn.wordpress.org/" title="基于WordPress，一个优美、先进的个人信息发布平台。">
         WordPress.org
        </a>
       </li>
      </ul>
     </div>
    </aside>
    <div class="clear">
    </div>
   </div>
   <!--main-->
   ﻿
   <footer id="dibu">
    <div class="about">
     <div class="right">
      <ul class="menu" id="menu-%e5%ba%95%e9%83%a8%e8%8f%9c%e5%8d%95">
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-18024" id="menu-item-18024">
        <a href="http://dataunion.org/category/partners">
         合作伙伴
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-20881" id="menu-item-20881">
        <a href="http://dataunion.org/contribute">
         文章投稿
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20872" id="menu-item-20872">
        <a href="http://dataunion.org/category/%e5%8a%a0%e5%85%a5%e6%95%b0%e7%9b%9f">
         加入数盟
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-22441" id="menu-item-22441">
        <a href="http://dataunion.org/f-links">
         友情链接
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-20874" id="menu-item-20874">
        <a href="http://dataunion.org/aboutus">
         关于数盟
        </a>
       </li>
      </ul>
      <p class="banquan">
       数盟社区        ，
        做最棒的数据科学社区
      </p>
     </div>
     <div class="left">
      <ul class="bottomlist">
       <li>
        <a href="http://weibo.com/DataScientistUnion  " target="_blank" 　title="">
         <img src="http://dataunion.org/wp-content/themes/yzipi/images/weibo.png"/>
        </a>
       </li>
       <li>
        <a class="cd-popup-trigger" href="http://dataunion.org/19559.html#0">
         <img src="http://dataunion.org/wp-content/themes/yzipi/images/weixin.png"/>
        </a>
       </li>
      </ul>
      <div class="cd-popup">
       <div class="cd-popup-container">
        <h1>
         扫描二维码,加微信公众号
        </h1>
        <img src="http://dataunion.org/wp-content/themes/yzipi/images/2014-12-06-1515289049.png"/>
        <a class="cd-popup-close" href="http://dataunion.org/19559.html">
        </a>
       </div>
       <!-- cd-popup-container -->
      </div>
      <!-- cd-popup -->
     </div>
    </div>
    <!--about-->
    <div class="bottom">
     <a href="http://dataunion.org/">
      数盟社区
     </a>
     <a href="http://www.miitbeian.gov.cn/" rel="external nofollow" target="_blank">
      京ICP备14026740号
     </a>
     联系我们：
     <a href="mailto:contact@dataunion.org" target="_blank">
      contact@dataunion.org
     </a>
     <div class="tongji">
     </div>
     <!--bottom-->
     <div class="scroll" id="scroll" style="display:none;">
      ︿
     </div>
    </div>
   </footer>
   <!--dibu-->
  </div>
 </body>
</html>